package com.xuan.friend.controller.user;


import com.xuan.api.domain.vo.UserQuestionResultVo;
import com.xuan.common.core.controller.BaseController;
import com.xuan.common.core.domain.R;
import com.xuan.common.core.exception.BusinessException;
import com.xuan.friend.aop.CheckUserStatus;
import com.xuan.friend.domain.question.vo.QuestionDetailVo;
import com.xuan.friend.domain.user.dto.UserSubmitDTO;
import com.xuan.friend.service.user.UserQuestionService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/question")
public class UserQuestionController extends BaseController {


    @Resource
    private UserQuestionService userQuestionService;


    @CheckUserStatus
    @PostMapping("/submit")
    public R<UserQuestionResultVo> submit(@RequestBody UserSubmitDTO userSubmitDTO) {
        Long questionId = userSubmitDTO.getQuestionId();
        String userCode = userSubmitDTO.getUserCode();
        if (questionId == null || StringUtils.isBlank(userCode)) {
            throw new BusinessException("参数为空");
        }
        return R.ok(userQuestionService.submit(userSubmitDTO));
    }

    @CheckUserStatus
    @PostMapping("/rabbit/submit")
    public R<Void> rabbitSubmit(@RequestBody UserSubmitDTO userSubmitDTO) {
        Long questionId = userSubmitDTO.getQuestionId();
        String userCode = userSubmitDTO.getUserCode();
        if (questionId == null || StringUtils.isBlank(userCode)) {
            throw new BusinessException("参数为空");
        }
        return toR(userQuestionService.rabbitSubmit(userSubmitDTO));
    }


    @GetMapping("/exe/result")
    public R<UserQuestionResultVo> getExeResult(Long questionId, Long contestId, String currentTime) {
        if (questionId == null) {
            throw new BusinessException("参数为空");
        }
        return R.ok(userQuestionService.getExeResult(questionId, contestId, currentTime));
    }


    @PutMapping("/reset")
    public R<QuestionDetailVo> questionReset(Long questionId, Long contestId) {
        if (questionId == null) {
            throw new BusinessException("参数为空");
        }
        return R.ok(userQuestionService.questionReset(questionId, contestId));
    }
}
